$(function() {
	var orderList = {
		data: null, // post 到的数据
		cancelData: null,
		config: { // 参数设置
			post: {
				page: 1,
				pagesize: 20,
				type: 'all' //pendingpay  alreadypay all
			},
			page: {
				cpage: 1,
				totalpage: 1
			}
		},
		post: function(page) { // post 数据
			var _this = this;
			$.post('/userorder/ajaxOrderList', this.config.post, function(data) {

				data.data.list = resoLveCircle(data.data.list)
				var _data = data;
				_this.config.page.cpage = data.data.current_page;
				_this.config.page.totalpage = data.data.total_page;
				_this.data = {
						data: data.data.list
				};
				if(_this.config.page.totalpage > 1) {
					$('._page').pagination({
						pageCount: _this.config.page.totalpage,
						current: _this.config.page.cpage,
						coping: true,
						callback: function(data) {
							_this.config.post.page = data.getCurrent();
							_this.post();
						}
					});
				} else {
					$('._page').html('');
				}

				if(!_this.data.data[0]) {
					$('.order_list ._conter').html('暂无记录!');
					return false;
				}
				_this.setTab();
			})
			return this;
		},

		setTab: function(D) { // 设置数据
			var data = D || this.data;
			data.data[0].order_time;
			for(var i = 0; i < data.data.length; i++) {
				var time = new Date(data.data[i].order_time * 1000);

				function setTime(a) {
					if(a < 10) {
						return '0' + a
					} else {
						return a
					}
				}

				var strTime = time.getFullYear() + '-' + setTime(time.getMonth() + 1) + '-' + setTime(time.getDate()) + ' ' + setTime(time.getHours()) + ':' + setTime(time.getMinutes()) + ':' + setTime(time.getSeconds());
				data.data[i].order_time = strTime;
			}
			//console.log(data);
			var html = template('test', data);
			$('.order_list ._conter').html(html);
		},

		setEvent: function() { // 添加事件
			//1、tab切换
			var _this = this;
			$('.certification_list a').click(function() {
				_this.config.post.page = 1;
				switch(this.id) {
					case 'one1':
						_this.config.post.type = '';
						break;
					case 'one2':
						_this.config.post.type = 'pendingpay';
						break;
					case 'one3':
						_this.config.post.type = 'alreadypay';
						break;
				}
				$('.certification_list a').removeClass('hr');
				$(this).addClass('hr');
				_this.post();
			});

			//取消订单
			$(window).on('click', '._example', function() {
				$("#LoginBox").fadeIn(300);
				_this.config.cancelData = $(this).parents('table').prev().find('span:eq(1) b').text();
			})

			//关闭
			$(".close_btn").hover(function() {
				$(this).css({
					color: 'black'
				})
			}, function() {
				$(this).css({
					color: '#999'
				})
			}).on('click', function() {
				$("#LoginBox").fadeOut("fast");

			});

			$('.smtwo.fr').click(function() {
				$.post('/userorder/ajaxCancelOrder', {
					order_id: _this.config.cancelData
				}, function(data) {
					//console.log(1,data)//data是取消订单的订单编号 - -不是是这个
					data.status == 200 && $("#LoginBox").fadeOut(300) && _this.post(); // 如果取消成功
				});
			});

			$('._conter').on('click', '.payment a', function() {
				var order = $(this).parents('table').prev().find('span:eq(1) b').text();
				formSubmit({
					url: '/pay/keeppay',
					data: {
						order_id: order
					},
					type: 'post'
				});
			})
		},

		init: function() { // 初始化数据
			this.setEvent();
			this.post();
		}
	}

	orderList.init();

});

function formSubmit(data) {
	var formInnHTML = ['<form style="display:none" action="' + (data.url || '#') + '" method="' + (data.type || 'post') + '">']
	for(var k in data.data) {
		formInnHTML.push('<input type="hidden" name="' + k + '" value="' + data.data[k] + '" />');
	}
	formInnHTML.push('<input type="submit" /></form>');
	var submitFrom = $(formInnHTML.join(''))[0];
	$('body').append(submitFrom);
	submitFrom.submit();
}

//解析套餐周期
function resoLveCircle(dataArray) {
	//遍历数组，修改每一项的
	for(var i = 0, len = dataArray.length; i < len; i++) {
		if(dataArray[i].condition_json)
			dataArray[i].cl = JSON.parse(dataArray[i].condition_json).package_cycle
	}
	return dataArray
}